package cn.kshost.zqyg.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.kshost.zqyg.manager.listener.CategoryExcelListener;
import cn.kshost.zqyg.manager.listener.ExcelListener;
import cn.kshost.zqyg.manager.mapper.CategoryMapper;
import cn.kshost.zqyg.manager.service.CategoryService;
import cn.kshost.zqyg.model.entity.product.Category;
import cn.kshost.zqyg.model.vo.product.CategoryExcelVo;
import com.alibaba.excel.EasyExcel;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<Category> getCategoryListByParentId(int parentId) {
        //根据父节点id查询节点
        List<Category>  categories =  categoryMapper.selectCategoryListByParentId(parentId);

        for (Category category : categories) {
            //查询父节点为当前类别id节点 验证当前节点是否还有子节点
           Long count =  categoryMapper.countByParentId(category.getId());
           if (count > 0) {
               category.setHasChildren(true);
           }else {
               category.setHasChildren(false);
           }

        }
        return categories;
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        String fileName = null;
        try {
            //获取category列表数据
           List<Category> categories =  categoryMapper.selectAllCategory();
           //利用stream流将category转为ExcelVo
            List<CategoryExcelVo> categoryExcelVos = categories.stream().map(category -> {
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                BeanUtil.copyProperties(category, categoryExcelVo);
                return categoryExcelVo;
            }).collect(Collectors.toList());
            //设置MIME
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            //设置文件名称
            fileName = URLEncoder.encode("分类数据", "UTF-8");
            //设置响应编码;
            response.setCharacterEncoding("utf-8");
            //设置响应头
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class).sheet("商品分类").doWrite(categoryExcelVos);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            //将mapper传入监听器用于插入
            CategoryExcelListener categoryExcelListener = new CategoryExcelListener(categoryMapper);
            EasyExcel.read(file.getInputStream(), CategoryExcelVo.class, categoryExcelListener).sheet().doRead();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
